package com.ocom.payhandle.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.PayHandleUrl;
import com.ocom.common.entity.card.EntityXfRecordProcess;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.pay.ABCChargeConfirmVo;
import com.ocom.payhandle.mq.business.saveorder.SaveOrderProductService;
import com.ocom.payhandle.service.XfRecordProcessService;
import com.ocom.payhandle.vo.MqSaveOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@Slf4j
@RestController
public class ABCController {
    @Autowired
    private XfRecordProcessService xfRecordProcessService;

    @Autowired
    private SaveOrderProductService saveOrderProductService;

    @PostMapping(PayHandleUrl.URL_InsertEntityXfRecord)
    public Result insertEntityXfRecord(@RequestBody EntityXfRecordProcess entityXfRecordProcess){
        int i = xfRecordProcessService.insertEntityXfRecord(entityXfRecordProcess.getComId(), entityXfRecordProcess);
        return i <= 0 ? ResultUtil.error(ResultEnum.Err_2016): ResultUtil.success();
    }

    @PostMapping(PayHandleUrl.URL_abcChargeConfirm)
    public Result abcChargeConfirm(@RequestBody ABCChargeConfirmVo vo){
        Result<EntityXfRecordProcess> result =  xfRecordProcessService.abcChargeConfirm(vo.getComId(), vo);

        if(result.getCode()==200){
            //同步到成功表
            try {
                MqSaveOrderVo mqSaveOrderVo = new MqSaveOrderVo();
                mqSaveOrderVo.setComId(result.getData().getComId());
                mqSaveOrderVo.setId(result.getData().getId());
                mqSaveOrderVo.setMachNo(result.getData().getDeviceNo());
                saveOrderProductService.saveOrder(JSON.toJSONString(mqSaveOrderVo));
            } catch (Exception e) {
                log.error("农行缴费推送成功订单异常 z7kW6tqa 【{}】【{}】 ",result.getData().getOrderNo(),e);
            }
        }
        return  result;

    }

    @PostMapping(PayHandleUrl.URL_abcChargeFail)
    public Result abcChargeFail(@RequestParam("comId")Long comId, @RequestParam("orderNo") String orderNo){
        return xfRecordProcessService.abcChargeFail(comId, orderNo);
    }
}
